package org.extremengine.demo;

import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;

import org.extremengine.TimerThread;
import org.extremengine.common.ActiveBody;
import org.extremengine.common.RigidBody;
import org.extremengine.common.Terrain;
import org.extremengine.common.Velocity;
import org.extremengine.shape.Circle;
import org.extremengine.shape.ParallelRectangle;

public class PanelPingPong extends BasePanelWorld {
	ActiveBody ab;
	RigidBody ball;
	RigidBody[] bricks = new RigidBody[20];
	
	public PanelPingPong(DemoCallback demoCallback) {
		super(demoCallback);
	}
	
	@Override
	public void initDemoWorld() {
		super.initDemoWorld();
		
		// 无重力
		world.setGravityAccelaration(0);
		
		// 防止粘住
//		world.setSpeedValve(0);
		
		// 碰撞动能无损耗
		for (Iterator it = world.getTerrains().iterator(); it.hasNext();) {
			Terrain t = (Terrain) it.next();
			t.setElasticity(1);
		}
		
		//按键控制
		this.addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent e) {
				System.out.println("Key pressed: " + e.getKeyChar());
				super.keyPressed(e);
				if (e.getKeyCode() == KeyEvent.VK_LEFT) {
					ab.setV(new Velocity(20, 180));
				}
				else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
					ab.setV(new Velocity(20, 0));
				}
				else if(e.getKeyCode() == KeyEvent.VK_UP) {
					ab.setV(new Velocity(20, 90));
				}
				else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
					if (world.getStatus() == TimerThread.STATUS_RUNNING) {
						world.pause();
					}
					else if (world.getStatus() == TimerThread.STATUS_PAUSED) {
						world.resume();
					}
				}
			}

			@Override
			public void keyReleased(KeyEvent e) {
				super.keyReleased(e);
				ab.setV(new Velocity(0, 0));
			}

		});
		
		// 创建对象
		ab = world.createActiveBody(new Object(), 0, 1, 10, new ParallelRectangle(25, 35, 4, 5));
//		ab.setMovable(false);
		
		ball = world.createRigidBody(new Object(), 0, 1, 10, new Circle(30, 6, 1));
		ball.setV(new Velocity(20, 40));
		
		
		for (int i = 0; i < 10; i++) {
			int x1 = i * 5 + 3;
			int x2 = i * 5 + 3 + 4;
			bricks[i] = world.createRigidBody(new Object(), 0, 1, 10, new ParallelRectangle(x1, x2, 30, 31));
			bricks[i].setMovable(false);
		}
		
//		for (int i = 0; i < 10; i++) {
//			int x1 = i * 5 + 3;
//			int x2 = i * 5 + 3 + 4;
//			bricks[i + 10] = world.createRigidBody(new Object(), 0, 1, 10, new ParallelRectangle(x1, x2, 27, 28));
//			bricks[i + 10].setMovable(false);
//		}
	}

	
	@Override
	protected void handleCollision(long t, RigidBody rb1, RigidBody rb2) {
		super.handleCollision(t, rb1, rb2);
		if ((rb1 == ball && rb2 != ab)) {
			world.destroyRigidBody(rb2);
		}
		else if (rb1 != ab && rb2 == ball) {
			world.destroyRigidBody(rb1);
		}
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		if (!world.isRunning()) {
			drawRigidBody(g, ab);
			drawRigidBody(g, ball);
			for (int i = 0; i < bricks.length; i++) {
				drawRigidBody(g, bricks[i]);
			}
		}
	}

}
